package com.ly.hotel.view.common;

import java.lang.ref.WeakReference;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.webagent.LyAgent;

public class MyWebDialog extends AlertDialog {
	private Context context;

	private WebView mWebView;
	LinearLayout mWebViewContainer;
	private LyAgent lyAgent = null;
	protected MyApplication app;
	private String msg;
	private Integer msgType = 0;
	private ThisHandler handlerMsg = new ThisHandler(this);
	
	
	public MyWebDialog(Context context, int theme) {
	    super(context, theme);
	    this.context = context;
	}

	public MyWebDialog(Context context) {
	    super(context);
	    this.context = context;
	}

	  
	private class WebViewClientDemo extends WebViewClient {
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			System.out.println("onReceivedError");
/*			view.stopLoading();
			view.clearView();
			Message msg = handlerMsg.obtainMessage();// 发送通知，加入线程
			msg.what = 3;// 失败就直接关闭
			handlerMsg.sendMessage(msg);// 通知发送！
*/		}
 
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}

		@Override
		// 在WebView中而不是默认浏览器中显示页面
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}
	   
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_webview_layout);
		  
		mWebViewContainer = (LinearLayout)findViewById(R.id.msgWebView);
		mWebView          = new WebView(context);
		mWebViewContainer.addView(mWebView);
		
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setSupportMultipleWindows(true);
		mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mWebView.setHorizontalScrollBarEnabled(false); // 设置水平滚动条
		mWebView.setVerticalScrollBarEnabled(false); // 设置竖直滚动条
		mWebView.setBackgroundColor(0); // 设置背景色
		mWebView.setFocusableInTouchMode(false);
		if (mWebView.isHardwareAccelerated()) {
			System.out.println("开启了硬件加速");
		}
		mWebView.setOnLongClickListener(new View.OnLongClickListener() { // 去掉长按
			@Override
			public boolean onLongClick(View view) {
				return true;
			}
		});
		  
		mWebView.setWebViewClient(new WebViewClientDemo());

		mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                view.requestFocus();
            }
        });
		// 把LyAgent暴露出WebView
		lyAgent = new LyAgent();
		lyAgent.initial(mWebView, context);
		mWebView.addJavascriptInterface(lyAgent, "LyAgent");

		setOnShowListener(new OnShowListener() {
			
			@Override
			public void onShow(DialogInterface arg0) {
				// TODO Auto-generated method stub
				handlerMsg.sendEmptyMessageDelayed(msgType, 200);
			}
		});
		
//		handlerMsg.sendEmptyMessageDelayed(3, 5000);
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Integer getMsgType() {
		return msgType;
	}

	public void setMsgType(Integer msgType) {
		this.msgType = msgType;
	}

	/**
	 * handler处理消息机制
	 */
	private static class ThisHandler extends Handler {
		private WeakReference<MyWebDialog> wrActivity;

		public ThisHandler(MyWebDialog activity) {
			wrActivity = new WeakReference<MyWebDialog>(activity);
		}

		@Override
		public void handleMessage(Message message) {
			MyWebDialog act = wrActivity.get();
			if (act != null) {
				switch (message.what) {
				case 0:
					Log.i("rtmsg", "显示html:" +act.msg);
					act.mWebView.getSettings().setDefaultTextEncodingName("UTF-8") ;
					act.mWebView.loadData(act.msg, "text/html; charset=UTF-8", null);
					break;	
					
				case 1:
					act.mWebView.clearCache(false);
					act.mWebView.loadUrl("file:///android_asset/www/tips.html");
					break;
					
				case 2:
					Log.i("myweb", "加载页面:" + act.msg);
					act.mWebView.clearCache(false);
					act.mWebView.loadUrl(act.msg);
					break;
					
				case 3:
					act.dismiss();
					break;					
				}
			}
		}
	}
}
